home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12109 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  55 lines

  1. Path: ix.netcom.com!news
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: extern consts
  5. Date: Mon, 18 Mar 1996 12:49:47 GMT
  6. Organization: Netcom
  7. Message-ID: <314d5987.219100870@nntp.ix.netcom.com>
  8. References: <4idbcv$ue2@news7.erols.com> <314c2077.138956468@nntp.ix.netcom.com> <4ii7h9$bq6@news6.erols.com>
  9. NNTP-Posting-Host: ix-dc6-01.ix.netcom.com
  10. X-NETCOM-Date: Mon Mar 18  6:48:19 AM CST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. Chris Cobb <ccobb@cseg.com> wrote:
  14.  
  15. > miker3@ix.netcom.com (Mike Rubenstein) wrote:
  16. > >
  17. > >It's not legal.  extern const is legal, but in any compilation unit
  18. > >that does not see the initializer the const variable is not a constant
  19. > >expression.
  20. > >
  21. > Well, your comment makes sense.  However, in a way it seem 
  22. > self-contradictory.  If a const can be extern, then it no longer is a 
  23. > const: you've basically externed away constness.  But if thats what 
  24. > externinig a const does, it doesn't make sense to allow it at all (which 
  25. > is what happens on most compilers.)
  26.  
  27. Others have already given some good answers, but I'll add that an
  28. extern const (without an initializer) has the same semantics as a
  29. const in C.  In C a const variable is never a constant expression.  In
  30. C++ it is a constant expression as long as it is initialized with a
  31. constant expression and the initialization is seen in the compilation
  32. unit.
  33.  
  34. In C and C++ a const is a VARIABLE that may not be changed.  It may be
  35. used anywhere a variable is permitted as long as it is not changed.
  36.  
  37. In C++, but not C, a const that is initialized with a constant
  38. expression may also be used whereever a constant expression is
  39. permitted.  This adds tremendously to the usefulness of the concept,
  40. but there still is some use for const variables.
  41.  
  42. Note that there are other things than being extern that can prevent a
  43. const from being used as a constant expression.  For example,
  44.  
  45.     int foo();
  46.     const int bar = foo();
  47.  
  48. is legal (in C it is legal only in function scope -- in C++ it is
  49. legal elsewhere).  However, bar cannot be used where a constant
  50. expression is required since it is not initialized with a constant
  51. expression.
  52.  
  53. Michael M Rubenstein
  54.